<template>
  <div class="user-container">
    <el-container>
      <el-header>
        <el-menu mode="horizontal" router :default-active="activeIndex" class="header-menu">
          <el-menu-item index="/user">
            <el-icon><HomeFilled /></el-icon>系统首页
          </el-menu-item>
          <el-menu-item index="/user/doctor-intro">
            <el-icon><OfficeBuilding /></el-icon>医院简介
          </el-menu-item>
          <el-menu-item index="/user/appointment">
            <el-icon><Calendar /></el-icon>预约挂号
          </el-menu-item>
          <el-menu-item index="/user/notice">
            <el-icon><Bell /></el-icon>医院公告
          </el-menu-item>
          <el-menu-item index="/user/feedback">
            <el-icon><ChatDotRound /></el-icon>问题反馈
          </el-menu-item>
          <div class="flex-grow"></div>
          <div class="user-dropdown">
            <el-dropdown>
              <div class="user-info">
                <el-avatar :size="28" :icon="UserFilled"></el-avatar>
                <span class="username">{{ userName }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="goToUserCenter">
                    <el-icon><User /></el-icon>个人中心
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-menu>
      </el-header>
      <el-main>
        <router-view />
      </el-main>
      <el-footer>
        <div class="footer-content">
          © 2023 医院排班系统 - 用户端
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { 
  HomeFilled, 
  OfficeBuilding, 
  Calendar, 
  Bell, 
  ChatDotRound, 
  User, 
  UserFilled,
  ArrowDown,
  SwitchButton
} from '@element-plus/icons-vue'
import { logout } from '@/utils/auth'

const router = useRouter()
const activeIndex = ref('/user')
const userName = ref('用户')

// 获取用户信息
onMounted(() => {
  const name = localStorage.getItem('userName')
  if (name) {
    userName.value = name
  }
})

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    logout()
  }).catch(() => {
    // 取消退出
  })
}

// 跳转到个人中心
const goToUserCenter = () => {
  router.push('/user/user-center')
}
</script>

<style scoped>
.user-container {
  height: 100%;
}

.el-container {
  height: 100%;
}

.el-header {
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-menu {
  width: 100%;
}

.flex-grow {
  flex-grow: 1;
}

.user-dropdown {
  display: flex;
  align-items: center;
  height: 60px;
  padding: 0 15px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 5px;
  font-size: 14px;
}

.el-main {
  padding: 20px;
  background-color: #f5f7fa;
}

.el-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60px;
  background-color: #f5f5f5;
}

.footer-content {
  text-align: center;
  color: #606266;
}
</style> 